import type {RouteRecordRaw} from 'vue-router'
import {createRouter, createWebHistory} from 'vue-router'

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        name: 'Home',
        component: () => import('@/pages/HomePage.vue'),
        meta: {title: '首页'}
    },
    {
        path: '/categories',
        name: 'Categories',
        component: () => import('@/pages/category/CategoriesPage.vue'),
        meta: {title: '分类'}
    },
    {
        path: '/favorites',
        name: 'Favorites',
        component: () => import('@/pages/favorite/FavoritesPage.vue'),
        meta: {title: '我的收藏'}
    },
    {
        path: '/profile',
        name: 'Profile',
        component: () => import('@/pages/user/UserPage.vue'),
        meta: {title: '个人中心'}
    },
    {
        path: '/recipe/:id',
        name: 'RecipeDetail',
        component: () => import('@/pages/recipes/RecipeDetailPage.vue'),
        meta: {title: '食谱详情'}
    },
    {
        path: '/add-recipe',
        name: 'AddRecipe',
        component: () => import('@/pages/recipes/AddRecipePage.vue'),
        meta: {title: '添加食谱'}
    },
    {
        path: '/search',
        name: 'Search',
        component: () => import('@/pages/SearchPage.vue'),
        meta: {title: '搜索'}
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    // 设置页面标题
    if (to.meta.title) {
        document.title = `${to.meta.title} - 美味佳肴`
    }
    next()
})

export default router
